import {ElLoading} from 'element-plus';
/**
    定义Loading组件的加载状态
*/
const LoadingInstance={
    _target:null as any,    // 存储目标对象
    _counter:0,    // 计数器
}
/**请求加载状态显示*/
export class AxiosLoading {
    // 显示加载
    showLoading = (target:any) => {
        //是否正在加载
        if(this.isLoading(target)){
            // 加载配置
            loadingOptions=target?target.loadingOptions:{};
            // 计数器+1
            LoadingInstance._counter++;
            // 首次加载
            if(LoadingInstance._counter===1){
                LoadingInstance._target=ElLoading.service(loadingOptions);
                console.log('显示加载');
            }
        }
    }
    // 隐藏加载
    hideLoading = (target:any) => {
        if(this.isLoading(target)&&LoadingInstance._counter>0) {
            LoadingInstance._counter--;
        }
        if(LoadingInstance._counter<=0){
            LoadingInstance._counter=0;
            LoadingInstance._target.close();
            LoadingInstance._target=null;
        }
    }
    // 是否正在加载
    isLoading = (target?:any) => {
        return target?.loading?target.loading:false;
    }
}